# coding: utf-8

# In[1]:


import json
from ast import *


def vis(node, lev=0):
    if isinstance(node, AST):
        res = {}
        for (k, v) in iter_fields(node):
            t = vis(lev=lev + 2, node=v)
            res[k] = t
        return {node.__class__.__name__: res}
    elif isinstance(node, list):
        res = []
        for x in node:
            res.append(vis(x, lev=lev + 1, ))
        return res
    else:
        return str(node)


with open('input.code') as f:
    file_content = ''.join(f.readlines())
    result = vis(parse(file_content))
    with open('grammar.json', mode='w') as f2:
        f2.write(json.dumps(result))
